package gisdcl.geoprocessors;

import gisdcl.schools.SchoolDistrictGeoprocessorsResource;

import java.util.logging.Logger;

import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

/*
 * Property address
 	6386 Lakeview Ct, West Bloomfield, MI 48323
	
	lat long 42.57259,-83.40997
 coord: (-83.410199, 42.572953, NaN) 
 <Point><coordinates>-83.4103540,42.5724580,0</coordinates></Point> 
 
 <SCHOOLDISTRICT ID="32" FIPS="26125" UNIFIED="35160" NAME="WALLED LAKE CONSOLIDATED SCHOOLS" GEOM="POLYGON (( -83.463184 42.48845,-83.46389 42.488438,-83.464989 42.48842,-83.466371 42.488397,-83.466421 42.490419,-83.46645 42.491615,-83.466513 42.494225,-83.466546 42.495682,-83.466708 42.502845,-83.472057 42.502827,-83.472077 42.504248,-83.472088 42.505052,-83.472102 42.506026,-83.472118 42.507109,-83.472136 42.508334,-83.472036 42.509234,-83.471936 42.510134,-83.472735 42.510033,-83.473135 42.510133,-83.474336 42.510033,-83.476636 42.509933,-83.477736 42.509933,-83.478429 42.50991,-83.481617 42.509803,-83.490337 42.509512,-83.494964 42.509357,-83.496774 42.509297,-83.496774 42.50929,-83.496769 42.50868,-83.496742 42.505699,-83.506571 42.505631,-83.506569 42.504774,-83.506566 42.503829,-83.506565 42.503391,-83.506562 42.502241,-83.506561 42.501955,-83.508598 42.501943,-83.510214 42.501933,-83.509578 42.494574,-83.511537 42.494535,-83.512436 42.494434,-83.512836 42.494434,-83.513536 42.494234,-83.515236 42.494334,-83.515637 42.494235,-83.519136 42.494234,-83.533837 42.493733,-83.535337 42.493733,-83.535837 42.493633,-83.540637 42.493533,-83.540737 42.495233,-83.535837 42.494233,-83.535837 42.494951,-83.535837 42.495233,-83.536037 42.496533,-83.536037 42.497733,-83.536137 42.498633,-83.536038 42.498934,-83.536138 42.500456,-83.538309 42.500461,-83.539954 42.500465,-83.540782 42.500467,-83.549063 42.500486,-83.55135 42.500491,-83.551354 42.501009,-83.551404 42.507571,-83.556182 42.50756,-83.556437 42.511633,-83.557139 42.523734,-83.557438 42.529233,-83.557638 42.531133,-83.557652 42.531305,-83.557938 42.534733,-83.558138 42.537233,-83.558239 42.548432,-83.55834 42.549533,-83.55844 42.552533,-83.557478 42.55257,-83.538625 42.553301,-83.537838 42.553332,-83.538038 42.556632,-83.53811 42.557971,-83.538238 42.560332,-83.538373 42.567686,-83.536394 42.567498,-83.534639 42.567332,-83.532394 42.567099,-83.532339 42.567132,-83.531873 42.567025,-83.530503 42.563965,-83.529053 42.560725,-83.529052 42.560575,-83.529015 42.56053,-83.527412 42.559882,-83.522112 42.557735,-83.522135 42.559425,-83.522113 42.561377,-83.519897 42.561371,-83.519821 42.564975,-83.519432 42.564971,-83.518977 42.564967,-83.518648 42.564964,-83.518738 42.565732,-83.518862 42.568648,-83.519285 42.575945,-83.519395 42.576118,-83.519509 42.576391,-83.51954 42.577333,-83.51991 42.58277,-83.518869 42.582754,-83.516466 42.582795,-83.51492 42.582832,-83.512425 42.582891,-83.511394 42.582916,-83.510491 42.582937,-83.510722 42.590189,-83.510883 42.594039,-83.511034 42.597574,-83.511383 42.60904,-83.515444 42.609035,-83.517183 42.609033,-83.517219 42.612671,-83.509576 42.612953,-83.504739 42.613131,-83.500939 42.613131,-83.499239 42.613031,-83.498139 42.613131,-83.497039 42.613231,-83.493731 42.613231,-83.493739 42.615131,-83.493494 42.61693,-83.492748 42.616943,-83.490839 42.616975,-83.490839 42.617331,-83.490739 42.617931,-83.490539 42.618331,-83.490139 42.618731,-83.489939 42.619131,-83.490753 42.620597,-83.485962 42.620572,-83.482982 42.620557,-83.481504 42.62063,-83.481511 42.623425,-83.48144 42.624332,-83.481639 42.627231,-83.48159 42.627928,-83.480184 42.627953,-83.479126 42.627972,-83.478139 42.628026,-83.475257 42.628107,-83.470238 42.628249,-83.470198 42.629314,-83.470167 42.630132,-83.470091 42.630682,-83.470117 42.631301,-83.470119 42.631664,-83.470654 42.631673,-83.471625 42.631688,-83.473074 42.631712,-83.476131 42.631721,-83.476094 42.63365,-83.476072 42.634786,-83.476066 42.635084,-83.476439 42.635031,-83.479834 42.635049,-83.4816 42.635033,-83.48159 42.635648,-83.481576 42.636554,-83.481569 42.637031,-83.481548 42.638353,-83.481545 42.638538,-83.481539 42.638931,-83.481539 42.639631,-83.48148 42.640172,-83.481539 42.640231,-83.481476 42.640336,-83.481466 42.640769,-83.481452 42.64137,-83.481841 42.641627,-83.479034 42.644658,-83.47785 42.645937,-83.475939 42.64563,-83.4733 42.645346,-83.472239 42.645231,-83.470239 42.645231,-83.46818 42.645238,-83.467441 42.64523,-83.467437 42.644812,-83.467436 42.644701,-83.467418 42.642722,-83.466254 42.642741,-83.462159 42.642806,-83.46054 42.642832,-83.458088 42.642896,-83.452992 42.643006,-83.452259 42.643034,-83.452195 42.639734,-83.452124 42.636074,-83.442239 42.636232,-83.442213 42.635493,-83.442195 42.634995,-83.442177 42.634475,-83.442144 42.633533,-83.442077 42.631651,-83.442067 42.631374,-83.442028 42.630257,-83.441878 42.626029,-83.441713 42.621351,-83.446453 42.621181,-83.446456 42.620836,-83.446481 42.617961,-83.441611 42.617998,-83.441602 42.61757,-83.44158 42.616582,-83.441539 42.614832,-83.441539 42.614291,-83.441539 42.614032,-83.441738 42.613531,-83.441538 42.613031,-83.441358 42.610659,-83.432656 42.610697,-83.432626 42.605763,-83.428705 42.605741,-83.428575 42.606874,-83.424823 42.606849,-83.422925 42.606836,-83.42295 42.605789,-83.423087 42.600099,-83.423115 42.598933,-83.423124 42.598552,-83.423137 42.598132,-83.423166 42.596824,-83.422535 42.596828,-83.41983 42.596843,-83.418736 42.596849,-83.418708 42.595209,-83.418702 42.594861,-83.421568 42.593041,-83.422297 42.592578,-83.422257 42.590098,-83.421637 42.589832,-83.420844 42.589568,-83.419876 42.589245,-83.416836 42.588232,-83.416107 42.588261,-83.415223 42.588296,-83.415304 42.585881,-83.41526 42.585552,-83.415279 42.584008,-83.417236 42.584032,-83.418736 42.584032,-83.420113 42.584021,-83.421064 42.584007,-83.421503 42.584004,-83.421628 42.579626,-83.421643 42.579083,-83.42143 42.578969,-83.418536 42.577432,-83.416836 42.576332,-83.416836 42.574732,-83.416246 42.574074,-83.415836 42.573832,-83.415562 42.57367,-83.415258 42.573626,-83.414367 42.573837,-83.408773 42.574366,-83.401084 42.574624,-83.397221 42.574629,-83.393269 42.574866,-83.39181 42.574982,-83.390799 42.575525,-83.390714 42.575762,-83.381098 42.575775,-83.381098 42.575412,-83.381098 42.575191,-83.381101 42.572687,-83.381135 42.571233,-83.38113 42.569855,-83.381126 42.568899,-83.381118 42.568127,-83.381095 42.566038,-83.381077 42.56547,-83.381063 42.565008,-83.381109 42.564419,-83.38403 42.564474,-83.385062 42.564493,-83.385027 42.56322,-83.385036 42.563134,-83.38502 42.562536,-83.385 42.56186,-83.389729 42.561903,-83.394522 42.561946,-83.394541 42.560993,-83.394566 42.559753,-83.394489 42.556205,-83.394419 42.553013,-83.394399 42.552101,-83.394394 42.551863,-83.39433 42.54893,-83.397665 42.548891,-83.398514 42.548873,-83.399942 42.548844,-83.399881 42.547387,-83.399771 42.544758,-83.399726 42.543686,-83.399704 42.543163,-83.399636 42.541534,-83.39941 42.537793,-83.394611 42.537814,-83.394578 42.534327,-83.387172 42.534339,-83.384444 42.534343,-83.384406 42.531063,-83.384363 42.52817,-83.384354 42.527564,-83.38693 42.527473,-83.387497 42.527453,-83.38877 42.527402,-83.390615 42.527329,-83.392242 42.527273,-83.395362 42.527188,-83.395457 42.527185,-83.396905 42.527137,-83.398719 42.527077,-83.399058 42.527076,-83.399708 42.527047,-83.402235 42.526934,-83.403195 42.526934,-83.405735 42.526934,-83.411061 42.5268,-83.411622 42.526786,-83.411989 42.526777,-83.414568 42.526713,-83.418557 42.526613,-83.418519 42.525846,-83.418466 42.52476,-83.41828 42.520766,-83.418179 42.518574,-83.418113 42.517149,-83.418059 42.515984,-83.417941 42.513454,-83.417897 42.512496,-83.41788 42.512126,-83.422068 42.512013,-83.423044 42.511984,-83.425493 42.511911,-83.427605 42.511853,-83.428888 42.511814,-83.430475 42.511769,-83.432969 42.51169,-83.433395 42.511676,-83.435319 42.511626,-83.436735 42.51159,-83.436736 42.511235,-83.436613 42.508518,-83.436467 42.505266,-83.436403 42.503845,-83.443718 42.503799,-83.443598 42.496484,-83.440949 42.496572,-83.440993 42.493637,-83.44103 42.492343,-83.444083 42.492354,-83.444734 42.492356,-83.444795 42.492356,-83.445408 42.492358,-83.445322 42.488747,-83.446625 42.488725,-83.45606 42.488568,-83.461622 42.488476,-83.463184 42.48845))  "  />
 */

public class GeoprocessorsTesterResource extends ServerResource {
	private static final Logger log = Logger.getLogger(GeoprocessorsTesterResource.class.getName());
	
	@Get
	public Representation getSchoolDistrict(Representation entity){
		//String homeAddress = getRequestAttributes().get("homeaddress").toString();
		try {
			GeometryFactory gf = new GeometryFactory();
			WKTReader reader = new WKTReader( gf );
			
			//Polygon polygon = (Polygon) reader.read("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))");
			Polygon polygon = (Polygon) reader.read("POLYGON (( -83.463184 42.48845,-83.46389 42.488438,-83.464989 42.48842,-83.466371 42.488397,-83.466421 42.490419,-83.46645 42.491615,-83.466513 42.494225,-83.466546 42.495682,-83.466708 42.502845,-83.472057 42.502827,-83.472077 42.504248,-83.472088 42.505052,-83.472102 42.506026,-83.472118 42.507109,-83.472136 42.508334,-83.472036 42.509234,-83.471936 42.510134,-83.472735 42.510033,-83.473135 42.510133,-83.474336 42.510033,-83.476636 42.509933,-83.477736 42.509933,-83.478429 42.50991,-83.481617 42.509803,-83.490337 42.509512,-83.494964 42.509357,-83.496774 42.509297,-83.496774 42.50929,-83.496769 42.50868,-83.496742 42.505699,-83.506571 42.505631,-83.506569 42.504774,-83.506566 42.503829,-83.506565 42.503391,-83.506562 42.502241,-83.506561 42.501955,-83.508598 42.501943,-83.510214 42.501933,-83.509578 42.494574,-83.511537 42.494535,-83.512436 42.494434,-83.512836 42.494434,-83.513536 42.494234,-83.515236 42.494334,-83.515637 42.494235,-83.519136 42.494234,-83.533837 42.493733,-83.535337 42.493733,-83.535837 42.493633,-83.540637 42.493533,-83.540737 42.495233,-83.535837 42.494233,-83.535837 42.494951,-83.535837 42.495233,-83.536037 42.496533,-83.536037 42.497733,-83.536137 42.498633,-83.536038 42.498934,-83.536138 42.500456,-83.538309 42.500461,-83.539954 42.500465,-83.540782 42.500467,-83.549063 42.500486,-83.55135 42.500491,-83.551354 42.501009,-83.551404 42.507571,-83.556182 42.50756,-83.556437 42.511633,-83.557139 42.523734,-83.557438 42.529233,-83.557638 42.531133,-83.557652 42.531305,-83.557938 42.534733,-83.558138 42.537233,-83.558239 42.548432,-83.55834 42.549533,-83.55844 42.552533,-83.557478 42.55257,-83.538625 42.553301,-83.537838 42.553332,-83.538038 42.556632,-83.53811 42.557971,-83.538238 42.560332,-83.538373 42.567686,-83.536394 42.567498,-83.534639 42.567332,-83.532394 42.567099,-83.532339 42.567132,-83.531873 42.567025,-83.530503 42.563965,-83.529053 42.560725,-83.529052 42.560575,-83.529015 42.56053,-83.527412 42.559882,-83.522112 42.557735,-83.522135 42.559425,-83.522113 42.561377,-83.519897 42.561371,-83.519821 42.564975,-83.519432 42.564971,-83.518977 42.564967,-83.518648 42.564964,-83.518738 42.565732,-83.518862 42.568648,-83.519285 42.575945,-83.519395 42.576118,-83.519509 42.576391,-83.51954 42.577333,-83.51991 42.58277,-83.518869 42.582754,-83.516466 42.582795,-83.51492 42.582832,-83.512425 42.582891,-83.511394 42.582916,-83.510491 42.582937,-83.510722 42.590189,-83.510883 42.594039,-83.511034 42.597574,-83.511383 42.60904,-83.515444 42.609035,-83.517183 42.609033,-83.517219 42.612671,-83.509576 42.612953,-83.504739 42.613131,-83.500939 42.613131,-83.499239 42.613031,-83.498139 42.613131,-83.497039 42.613231,-83.493731 42.613231,-83.493739 42.615131,-83.493494 42.61693,-83.492748 42.616943,-83.490839 42.616975,-83.490839 42.617331,-83.490739 42.617931,-83.490539 42.618331,-83.490139 42.618731,-83.489939 42.619131,-83.490753 42.620597,-83.485962 42.620572,-83.482982 42.620557,-83.481504 42.62063,-83.481511 42.623425,-83.48144 42.624332,-83.481639 42.627231,-83.48159 42.627928,-83.480184 42.627953,-83.479126 42.627972,-83.478139 42.628026,-83.475257 42.628107,-83.470238 42.628249,-83.470198 42.629314,-83.470167 42.630132,-83.470091 42.630682,-83.470117 42.631301,-83.470119 42.631664,-83.470654 42.631673,-83.471625 42.631688,-83.473074 42.631712,-83.476131 42.631721,-83.476094 42.63365,-83.476072 42.634786,-83.476066 42.635084,-83.476439 42.635031,-83.479834 42.635049,-83.4816 42.635033,-83.48159 42.635648,-83.481576 42.636554,-83.481569 42.637031,-83.481548 42.638353,-83.481545 42.638538,-83.481539 42.638931,-83.481539 42.639631,-83.48148 42.640172,-83.481539 42.640231,-83.481476 42.640336,-83.481466 42.640769,-83.481452 42.64137,-83.481841 42.641627,-83.479034 42.644658,-83.47785 42.645937,-83.475939 42.64563,-83.4733 42.645346,-83.472239 42.645231,-83.470239 42.645231,-83.46818 42.645238,-83.467441 42.64523,-83.467437 42.644812,-83.467436 42.644701,-83.467418 42.642722,-83.466254 42.642741,-83.462159 42.642806,-83.46054 42.642832,-83.458088 42.642896,-83.452992 42.643006,-83.452259 42.643034,-83.452195 42.639734,-83.452124 42.636074,-83.442239 42.636232,-83.442213 42.635493,-83.442195 42.634995,-83.442177 42.634475,-83.442144 42.633533,-83.442077 42.631651,-83.442067 42.631374,-83.442028 42.630257,-83.441878 42.626029,-83.441713 42.621351,-83.446453 42.621181,-83.446456 42.620836,-83.446481 42.617961,-83.441611 42.617998,-83.441602 42.61757,-83.44158 42.616582,-83.441539 42.614832,-83.441539 42.614291,-83.441539 42.614032,-83.441738 42.613531,-83.441538 42.613031,-83.441358 42.610659,-83.432656 42.610697,-83.432626 42.605763,-83.428705 42.605741,-83.428575 42.606874,-83.424823 42.606849,-83.422925 42.606836,-83.42295 42.605789,-83.423087 42.600099,-83.423115 42.598933,-83.423124 42.598552,-83.423137 42.598132,-83.423166 42.596824,-83.422535 42.596828,-83.41983 42.596843,-83.418736 42.596849,-83.418708 42.595209,-83.418702 42.594861,-83.421568 42.593041,-83.422297 42.592578,-83.422257 42.590098,-83.421637 42.589832,-83.420844 42.589568,-83.419876 42.589245,-83.416836 42.588232,-83.416107 42.588261,-83.415223 42.588296,-83.415304 42.585881,-83.41526 42.585552,-83.415279 42.584008,-83.417236 42.584032,-83.418736 42.584032,-83.420113 42.584021,-83.421064 42.584007,-83.421503 42.584004,-83.421628 42.579626,-83.421643 42.579083,-83.42143 42.578969,-83.418536 42.577432,-83.416836 42.576332,-83.416836 42.574732,-83.416246 42.574074,-83.415836 42.573832,-83.415562 42.57367,-83.415258 42.573626,-83.414367 42.573837,-83.408773 42.574366,-83.401084 42.574624,-83.397221 42.574629,-83.393269 42.574866,-83.39181 42.574982,-83.390799 42.575525,-83.390714 42.575762,-83.381098 42.575775,-83.381098 42.575412,-83.381098 42.575191,-83.381101 42.572687,-83.381135 42.571233,-83.38113 42.569855,-83.381126 42.568899,-83.381118 42.568127,-83.381095 42.566038,-83.381077 42.56547,-83.381063 42.565008,-83.381109 42.564419,-83.38403 42.564474,-83.385062 42.564493,-83.385027 42.56322,-83.385036 42.563134,-83.38502 42.562536,-83.385 42.56186,-83.389729 42.561903,-83.394522 42.561946,-83.394541 42.560993,-83.394566 42.559753,-83.394489 42.556205,-83.394419 42.553013,-83.394399 42.552101,-83.394394 42.551863,-83.39433 42.54893,-83.397665 42.548891,-83.398514 42.548873,-83.399942 42.548844,-83.399881 42.547387,-83.399771 42.544758,-83.399726 42.543686,-83.399704 42.543163,-83.399636 42.541534,-83.39941 42.537793,-83.394611 42.537814,-83.394578 42.534327,-83.387172 42.534339,-83.384444 42.534343,-83.384406 42.531063,-83.384363 42.52817,-83.384354 42.527564,-83.38693 42.527473,-83.387497 42.527453,-83.38877 42.527402,-83.390615 42.527329,-83.392242 42.527273,-83.395362 42.527188,-83.395457 42.527185,-83.396905 42.527137,-83.398719 42.527077,-83.399058 42.527076,-83.399708 42.527047,-83.402235 42.526934,-83.403195 42.526934,-83.405735 42.526934,-83.411061 42.5268,-83.411622 42.526786,-83.411989 42.526777,-83.414568 42.526713,-83.418557 42.526613,-83.418519 42.525846,-83.418466 42.52476,-83.41828 42.520766,-83.418179 42.518574,-83.418113 42.517149,-83.418059 42.515984,-83.417941 42.513454,-83.417897 42.512496,-83.41788 42.512126,-83.422068 42.512013,-83.423044 42.511984,-83.425493 42.511911,-83.427605 42.511853,-83.428888 42.511814,-83.430475 42.511769,-83.432969 42.51169,-83.433395 42.511676,-83.435319 42.511626,-83.436735 42.51159,-83.436736 42.511235,-83.436613 42.508518,-83.436467 42.505266,-83.436403 42.503845,-83.443718 42.503799,-83.443598 42.496484,-83.440949 42.496572,-83.440993 42.493637,-83.44103 42.492343,-83.444083 42.492354,-83.444734 42.492356,-83.444795 42.492356,-83.445408 42.492358,-83.445322 42.488747,-83.446625 42.488725,-83.45606 42.488568,-83.461622 42.488476,-83.463184 42.48845))  ");
			Double polyArea = polygon.getArea();

			Coordinate coord = new Coordinate( -83.4103540, 42.5724580 );
			
			Point point = gf.createPoint( coord );
			
			Boolean isPointInPolygon = polygon.contains(point);
			
			return new StringRepresentation("TRUE ", MediaType.TEXT_PLAIN);
		} catch (ParseException e) {
			log.warning(e.getMessage());
			return new StringRepresentation("False ", MediaType.TEXT_PLAIN);
		}
	
		
		
	}

}
